<?php 
if(KT!='KhiThe') exit;
class categorys_controller extends categorys_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');

	}
	
	public function index_action()
	{
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$stop=1;
		$ID = isset($rowMenu['ID'])?$rowMenu['ID']:NULL;
		$resultHtml = $this->getCategory($ID,0,0,0,1);
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		return $strHtmlContent;	
	}
	
	public function add_action()
	{
		if($_GET['iframe']=='true')
		{
			
			/*Seo*/
			$this->lang['val_seo_key'] ='';
			$this->lang['val_title_page'] ='';
			$this->lang['val_keywords'] = '';
			$this->lang['val_description'] ='';
			$this->lang['val_seo_h1'] ='';
			$this->lang['val_seo_h2'] ='';
			$this->lang['val_seo_p'] ='';
			
			
			$this->lang['txt_value'] ='';
			$this->lang['active'] ='add';
			$this->lang['CategoryID'] ='';
			$this->lang['val_Order'] ='';
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$strHtmlContent = $this->khithe_display('editor');
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$stop=1;
			$resultHtml = $this->getCategory(0,'',0,0,0,1);
            $resultHtmlCom = $this->getCom();
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlcomp'=>$resultHtmlCom));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function edit_action()
	{
		if($_GET['iframe']=='true')
		{
			
			$resultMenu = parent::getMenu($this->valUrl(1));
			$rowMenu = mysql_fetch_assoc($resultMenu);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = $this->lang['editTitle'];
			$this->lang['txt_value'] = $rowMenu['Category'];
			$this->lang['active'] ='edit';
			$this->lang['CategoryID'] =$rowMenu['ID'];
			$this->lang['val_Order'] =$rowMenu['Order'];
			
			/*Seo*/
			$this->lang['val_seo_key'] =$rowMenu['SeoKey'];
			$this->lang['val_title_page'] =$rowMenu['TitlePage'];
			$this->lang['val_description'] =$rowMenu['Description'];
			$this->lang['val_keywords'] =$rowMenu['KeyWords'];
			$this->lang['val_seo_h1'] =$rowMenu['SeoH1'];
			$this->lang['val_seo_h2'] =$rowMenu['SeoH2'];
			$this->lang['val_seo_p'] =$rowMenu['SeoP'];

			$this->lang['h_menu_0'] = $rowMenu['MenuH']==0?'selected':null;
            $this->lang['v_menu_0'] = $rowMenu['MenuV']==0?'selected':null;
            $this->lang['is_page_0'] = $rowMenu['IsPage']==0?'selected':null;

			
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$resultHtml = $this->getCategory(0,0,0,$rowMenu['ParentID'],0,1);
            $resultHtmlCom = $this->getCom($rowMenu['comp']);
            $strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlcomp'=>$resultHtmlCom));
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function index_ajax_action(){
		
		switch ($this->valUrl(1))
		{
			case 'open':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::setCheckedMenu($ID,'true');
				}
			break;

			case 'order':

				$arrId = explode(',',$this->valUrl(2));
				$done = parent::setOrderMenu($arrId);
			break;

			case 'close':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::setCheckedMenu($ID,'false');
					if($done){
						$getCat = parent::getMenuProducts($ID);
						while( $row = mysql_fetch_assoc($getCat) )
						{
							parent::setCheckedMenu($row['ID'],'false');
						} mysql_free_result($getCat);
					}
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::delCheckedMenu($ID);
					if($done){
						$getDelCat = parent::getMenuProducts($ID);
						while( $row = mysql_fetch_assoc($getDelCat) )
						{
							parent::delCheckedMenu($row['ID']);
						} mysql_free_result($getDelCat);
					}
				}
			break;
			
			case 'edit':
				if(parent::mdcheckSeoKey($_POST['seo_key'])>0 && $_POST['seo_key'] != $_POST['seo_key_hi'])
				{
					return 'notOKSeo';
				}
				else{
					$done = parent::updateInsertCat($_POST);
				}
				
			break;
			
			
			case 'add':
                if(empty($_POST['seo_key'])){
                    $_POST['seo_key'] = $_POST['CategoryName'];
                }
				if(parent::mdcheckSeoKey($_POST['seo_key'])>0)
				{
					return 'notOKSeo';
				}
				else{

					$done = parent::updateInsertCat($_POST);
				}
			break;
		}
		
		if( $this->valUrl(1)!="" && isset($done) ){
			return $this->index_action();
			
		}else{
			return 'notOK';
		}
		
	}

    private function getCom($selectedID=null)
    {
        $resultHtml=NULL;
        $resultMenu = parent::getMenuCom();
        while( $rowMenu = mysql_fetch_assoc($resultMenu) )
        {
            $arrMenu = array(
                "cell_ID"=>$rowMenu['ComDir'],
                "cell_Value"=>$rowMenu['ComTitle'],
                "cell_selected"=>$selectedID==$rowMenu['ComDir']?' selected ':'',
            );
            $resultHtml .= $this->khithe_assign($this->arrBlockHtml['component'],$arrMenu);

        } mysql_free_result($resultMenu);

        return $resultHtml;
    }

	private function getCategory($ID, $Order, $indent, $selected, $stop, $ispage=NULL)
	{	
		$resultHtml=NULL; 
		$indent += 1;
		$resultMenu = parent::getMenuProducts($ID,$ispage);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$arrMenu = array(
				"cell_ID"=>$rowMenu['ID'],
				"cell_Value"=>str_repeat("&nbsp;&nbsp;", $indent).$rowMenu['Category'],
				"indent_num"=>$indent,
				"minus"=>'minus',
				"PID"=>$rowMenu['ParentID'],
				"cell_selected"=>$selected==$rowMenu['ID']?' selected ':'',
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$rowMenu['ID'].'" />',
				"cell_2"=>"MaDM_".$rowMenu['ID'],
				"cell_3"=>$rowMenu['Category'],
                "comp"=>$rowMenu['comp'],
				"OrderText"=>$this->lang['cat-order'].' '.($Order==0?'C':$Order).'.',
				"Order"=>$rowMenu['Order'],
				"cell_5"=>$rowMenu['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$rowMenu['Status']=='true'?"cell_groupitem$indent":"cell_groupitem_ex"
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['category'],$arrMenu);
			if(isset($rowMenu['ID'])&&$stop==1) {
                $strc =$this->getCategory($rowMenu['ID'], $rowMenu['Order'], $indent, $selected, $stop);
                $none = 'block';
                $resultHtml .= $this->khithe_assign($this->arrBlockHtml['div_child'], array('ID' => $rowMenu['ID'], 'div_child' => $strc, 'none' => $none));
            }
		} mysql_free_result($resultMenu);
		
		return $resultHtml;
	}


}
?>